package com.wuyu.ibatis.config;


import com.wuyu.ibatis.binding.MapperRegistry;
import com.wuyu.ibatis.session.SqlSession;

import java.util.HashSet;
import java.util.Properties;
import java.util.Set;

/**
 * 配置信息
 */
public class Configuration {
    /**
     * 数据库链接信息
     */
    protected Properties variables = new Properties();

    /**
     * 已经加载的mapper文件
     */
    protected final Set<String> loadedResources = new HashSet<>();

    /**
     * mapper注册表
     */
    protected final MapperRegistry mapperRegistry = new MapperRegistry(this);



    public Properties getVariables() {
        return variables;
    }

    public void setVariables(Properties variables) {
        this.variables = variables;
    }

    public void addLoadedResource(String resource) {
        loadedResources.add(resource);
    }

    public boolean isResourceLoaded(String resource) {
        return loadedResources.contains(resource);
    }

    public MapperRegistry getMapperRegistry() {
        return mapperRegistry;
    }

    /**
     * mapper注册
     * @param type
     * @param <T>
     */
    public <T> void addMapper(Class<T> type) {
        mapperRegistry.addMapper(type);
    }

    public <T> T getMapper(Class<T> type, SqlSession sqlSession) {
        return mapperRegistry.getMapper(type, sqlSession);
    }

    public boolean hasMapper(Class<?> type) {
        return mapperRegistry.hasMapper(type);
    }
}
